local PartnerEquip = class("PartnerEquip")

function PartnerEquip:ctor()
   
    self._strengthenLv = 1  -- 强化等级
    self._strengthenProbability = 10000 --强化成功率
    self._props = {}
    self._isUnlock = false
    self.itemBagType = 130
    self._quality = 1
    self._disableStrength = false
    self.itemBagType = Enums.ItemBagType.partnerEquip
    self._partner = nil
end

-- function PartnerEquip:initById(id)
--     assert(id ~= nil, " init PartnerEquip id is nil")
--     self._conf = GD:queryPartnerEquipById(id)
--     self._id = id
--     self:_initProp()
-- end

function PartnerEquip:initByConfig(conf)
    assert(conf ~= nil, " init PartnerEquip conf is nil")
    self._conf = conf
    self._id = tonumber(conf.ID)
    self:_initProp()
end

function PartnerEquip:_initProp()
    local arrs = string.split(self._conf["attrValue"], ";")
    for k, arrStr in ipairs(arrs) do
        local valueArr = string.split(arrStr, "|")
        if #valueArr == 2 then
            table.insert(self._props, {id = valueArr[1], maxVaule = tonumber(valueArr[2]) or 0})
        end
    end
    self._quality = tonumber(self._conf.quality)
end


function PartnerEquip:setUnlock(trueOrFalse)
    if self._isUnlock == false and self._isUnlock ~= trueOrFalse then
        self._isNewUnlock = true
    end
    self._isUnlock = trueOrFalse
end

function PartnerEquip:isUnlock()
    return self._isUnlock == true
end

function PartnerEquip:isNewUnlock()
    return self._isNewUnlock
end

function PartnerEquip:setNewUnlock(trueOrFalseOrNil)
    self._isNewUnlock = trueOrFalseOrNil
end

-- 更新数据
function PartnerEquip:updateData(value)
    self._strengthenLv = value.Lv
    self._strengthenProbability = value.probability
end

-- 获取格子位置
function PartnerEquip:getSlot()
    return tonumber(self._conf.sort)
end

function PartnerEquip:getStrengthenPropShowDataByLevel(lv)
    local sConf = GD:queryPartnerEquipStrengthenByLevel(lv)
    local props = {}

    local par = tonumber(sConf.par) or 0

    for k, v in pairs(self._props) do
        local name, value = GD:getPropShowDataAndHandlerColon(v.id, math.ceil(v.maxVaule * par / 10000))
        table.insert(props, {name = name or "", value = value or ""})
    end
    return props
end

function PartnerEquip:getCurStrengthenPropShowData()
    return self:getStrengthenPropShowDataByLevel(self._strengthenLv)
end

function PartnerEquip:getNextStrengthenPropShowData()
    return self:getStrengthenPropShowDataByLevel(self._strengthenLv + 1)
end

function PartnerEquip:isStrenthMaxLevel()
    local level = 1
    if self._partner then
        level = self._partner:getLevel()
    end
    return self._strengthenLv >= level + self:getEnhanceAddLv()
end

-- 获取强化需要
function PartnerEquip:getStrengthenUpCostByLevel(lv)
    local sConf, isMax = GD:queryPartnerEquipStrengthenByLevel(lv + 1)
    local valueArr = string.split(sConf.cost, "|")
    return {
        type = tonumber(valueArr[1]),
        id = tonumber(valueArr[2]),
        num = tonumber(valueArr[3]),
    }
end

function PartnerEquip:getIcon()
    return Res.PartnerEquipPath..self._conf.icon..".png"
end

function PartnerEquip:getIconBg()
	return Res.Qulity_Icon_Bg[self._quality]
end

function PartnerEquip:getName()
    return L(self._conf.name)
end


function PartnerEquip:getDesc()
    return L(self._conf.desc)
end

function PartnerEquip:getlocationString()
    return L(self._conf.type)
end

function PartnerEquip:getId()
    return self._id
end

function PartnerEquip:getQuality()
    return self._quality
end
-- debug 调试
function PartnerEquip:getDumpTable()
    local t = {
        name = self:getName(),
        _id = self._id,
        _strengthenLv = self._strengthenLv,
        _strengthenProbability = self._strengthenProbability,
        _isUnlock = self._isUnlock,
        _props = {}
    }
    t._props = self:getCurStrengthenPropShowData()
    return t
end


function PartnerEquip:getStrengthenLevel()
    return self._strengthenLv
end

function PartnerEquip:getStrengthenProbability()
    return self._strengthenProbability
end


function PartnerEquip:setDisableStrength(trueOrFalse)
    self._disableStrength = trueOrFalse
end

function PartnerEquip:isDisableStrength()
    return self._disableStrength
end

function PartnerEquip:getEnhanceAddLv()
    return tonumber(self._conf.enhanceAddLv)
end

function PartnerEquip:checkCanStrength()
    if not self:isUnlock() then
        return false, 1
    end

    if self:isStrenthMaxLevel() then
        return false, 2
    end
    
    local conf = self:getStrengthenUpCostByLevel(self._strengthenLv)
    local nowNum = UD:getItemAmountByTid(conf.id)
    -- local nowNum
    if nowNum >= conf.num then
        return true
    end
    return false, 3
end

function PartnerEquip:getSubMenu()
	return nil
end

function PartnerEquip:getResource()
	local resource = GD:getConvertResource(self._conf.getResource, Enums.ItemBagType.partnerEquip, self._id, self:getSubMenu())
	return resource
end


function PartnerEquip:setPartner(partner)
    self._partner = partner
end

function PartnerEquip:getPartner()
    return self._partner
end


return PartnerEquip